package Linked;

import java.io.LineNumberReader;

/**
 * Created by sunwei on 2017/10/9
 * Time:15:47
 */
public class LinkList {

    private Link first;

    public LinkList() {
        first = null;
    }

    public boolean isEmpty(){
        return (first==null);
    }

    /*
    插入链表节点，将新插入的链表节点后面指向原来的first节点，
     */
    public void InsertFirst(int id,Double dd){
        Link newLink= new Link(id,dd);
        newLink.next = first;
        first = newLink;
    }
    /*
    删除节点
     */
    public Link deleteFirst(){
        Link temp = first;
        first = first.next;
        return temp;
    }
    /*
    查找特定关键词的节点
     */
    public Link findLink(int key){
        Link current = first;
        while (current.iData!=key){
            if (current.next==null) {
                  return null;
            }else {
                current = current.next;
            }
        }
        return current;
    }

    /*
    删除含有特定关键词的节点
     */
    public Link delete(int key){
        Link current = first;
        Link previous = first;
        if (current.dData!=key) {
              if (current.next==null) {
                    return null;
              }else {
                  previous = current;
                  current = current.next;
              }
              if (current==first) {
                    first = first.next;
              }else {
                  previous.next = current.next;
              }
        }
        return current;
    }
    /*
    遍历显示节点
     */
    public void displayList(){
        System.out.print("List(first-->last): ");
        Link current = first;
        while (current.next!=null){
            current.displayLink();
            current = current.next;
        }
        System.out.println(" ");
    }


}
